/*-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License.You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
	
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------*/

package fever.plugin.manager.abstract
{
	import com.bourre.commands.FrontController;
	import com.bourre.plugin.AbstractPlugin;
	
	import fever.error.AbstractConstructorAccessException;
	import fever.plugin.manager.PluginManager;
	import fever.plugin.manager.controls.ActivatePlugin;
	import fever.plugin.manager.controls.DesactivatePlugin;
	import fever.plugin.manager.controls.RegisterPlugin;
	import fever.plugin.manager.controls.UnregisterPlugin;
	import fever.plugin.manager.events.PluginManagerEvent;
	import fever.plugin.manager.events.PluginManagerListener;
	import fever.plugin.manager.models.PluginManagerModel;	

	/**
	 * Plugins manager abstract implementation.
	 * 
	 * <p>Manager plugin is responsible of others context plugins 
	 * registration.<br />
	 * When a <strong>fever.ioc.plugin.RegistredPlugin</strong> plugin is loaded in 
	 * context, Manager know it.<br />
	 * Manager can so activate / desactivate / unregister them at runtime.</p>
	 * 
	 * <p>All model logic is already implemented, you have just to 
	 * implements view side using <strong>PluginManagerListener</strong> interface.
	 * 
	 * <p>Class is abstract, don't instanciate it, implements subclass to 
	 * use Plugin Manager API.</p>
	 * 
	 * @example
	 * <listing version="3.0">
	 * //TODO Sample
	 * </listing>
	 * 
	 * @see fever.ioc.manager.AbstractManagerPlugin#addManagerListener	 * @see fever.ioc.manager.AbstractManagerPlugin#removeManagerListener
	 * @see fever.ioc.manager.events.PluginManagerEvent
	 * @see fever.ioc.manager.events.PluginManagerListener	 * @see fever.ioc.manager.models.PluginManagerModel
	 * 
	 * @author Romain Ecarnot
	 */
	public class AbstractManagerPlugin extends AbstractPlugin 
		implements PluginManager
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _model : PluginManagerModel;
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Adds passed-in <code>view</code> as Plugin manager view.
		 * 
		 * @param view Plugin manager view to add
		 */
		public function addManagerView( view : PluginManagerListener ) : Boolean
		{
			return _model.addManagerListener( view );	
		}
		
		/**
		 * Removes passed-in <code>view</code> from Plugin manager views.
		 * 
		 * @param view Plugin manager view to remove
		 */
		public function removeManagerView( view : PluginManagerListener ) : Boolean
		{
			return _model.removeManagerListener( view );	
		}
		
		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
		
		/**
		 * Allows <strong>AbstractManagerPlugin</strong> extension.<br />
		 * Pass <code>getConstructorAccess()</code> as 1st parameter when calling 
		 * super constructor.
		 * 
		 * @example
		 * <listing version=3.0>
		 * 
		 * package
		 * {
		 * 	class MyPlugin extends AbstractManagerPlugin
		 * 	{
		 * 		public function MyPlugin( )
		 * 		{
		 * 			super( getContructorAccess() );
		 * 		}
		 * 	}
		 * } 
		 * </listing>
		 */
		protected final function getConstructorAccess() : AbstractAccess
		{ 
			return new AbstractAccess(); 
		}
			
		/**
		 * @private
		 * Private constructor as Abstract class
		 */
		public function AbstractManagerPlugin( access : AbstractAccess )
		{
			super( );
			
			if( access == null )
			{
				throw new AbstractConstructorAccessException( this );
			}
			else
			{
				var controller : FrontController = getController();
			
				controller.pushCommandClass( 
					PluginManagerEvent.onRegisterPluginEVENT, 
					RegisterPlugin 
				);
				controller.pushCommandClass( 
					PluginManagerEvent.onUnregisterPluginEVENT, 
					UnregisterPlugin 
				);
				controller.pushCommandClass( 
					PluginManagerEvent.onActivatePluginEVENT, 
					ActivatePlugin 
				);
				controller.pushCommandClass( 
					PluginManagerEvent.onDesactivatePluginEVENT, 
					DesactivatePlugin 
				);
				
				_model = new PluginManagerModel( this );
				
				pluginManagerChannel = getChannel();
			}
		}
	}
}

internal class AbstractAccess{}